// miniprogram/pages/statistics/index.js
Page({
  data: {
    totalPoints: 1250,
    rank: 2,
    totalUsers: 7,
    chartData: [],
    sceneType: '',
    sceneTypeName: ''
  },

  onLoad() {
    const app = getApp()
    this.setData({
      sceneType: app.globalData.sceneType,
      sceneTypeName: app.getSceneTypeName(app.globalData.sceneType)
    })
    this.loadChartData()
  },

  loadChartData() {
    // 模拟图表数据
    const chartData = [
      { date: '05-01', points: 25 },
      { date: '05-02', points: 30 },
      { date: '05-03', points: 15 },
      { date: '05-04', points: 40 },
      { date: '05-05', points: 20 },
      { date: '05-06', points: 35 },
      { date: '05-07', points: 45 }
    ]
    
    // 计算最大值，用于柱形图高度计算
    const maxPoints = Math.max(...chartData.map(item => item.points))
    // 为每个数据项添加高度属性
    const chartDataWithHeight = chartData.map(item => {
      return {
        ...item,
        height: maxPoints > 0 ? (item.points / maxPoints) * 250 : 0 // 250是图表区域的最大高度
      }
    })
    
    this.setData({ chartData: chartDataWithHeight })
  },

  viewRanking() {
    wx.navigateTo({
      url: '/pages/statistics/ranking'
    })
  },

  viewHistory() {
    wx.navigateTo({
      url: '/pages/statistics/history'
    })
  }
})